/**
 * 类名：HomeworkService
 * 创建日期： 2014-11-16 下午7:19:03
 * 
 */
package com.erms.domain;

import java.io.File;

import com.erms.bean.Homework;
import com.erms.bean.Task;
import com.erms.bean.User;
import com.erms.dao.HomeworkDAO;
import com.erms.util.TimeUtil;

/**
 * @author hmb
 *         备注：
 * 
 */
public class HomeworkService extends BaseService {
	private HomeworkDAO	homeworkDAO;
	private Homework	homework;

	public Homework getHomework() {

		return homework;
	}

	public void setHomework(Homework homework) {

		this.homework = homework;
	}

	public HomeworkDAO getHomeworkDAO() {

		return homeworkDAO;
	}

	public void setHomeworkDAO(HomeworkDAO homeworkDAO) {

		this.homeworkDAO = homeworkDAO;
	}

	/**
	 * 教师评阅作业
	 * 
	 * @param homeworkID 作业ID
	 * @param pass 是否通过
	 * @param score 分数
	 * @param evaluation 评语
	 * @return 操作是否成功
	 */
	public boolean correct(Integer homeworkID, Boolean pass, Float score,
			String evaluation) {

		boolean result = true;
		try {
			Homework homework = homeworkDAO.findById(homeworkID);
			if (homework == null) {
				logger.debug("没有找到对应作业");
				return false;
			}
			homework.setPass(pass.toString());
			homework.setEvaluation(evaluation);
			homework.setScore(score);
			homeworkDAO.merge(homework);
		} catch (Exception e) {
			// TODO: handle exception
			logger.debug("发生未知错误", e);
			result = false;
		}

		return result;

	}

	/**
	 * 更改作业，不修改附件
	 * 
	 * @param homeworkID 作业ID
	 * @param content 作业内容，可为空但木有意义
	 * @return 操作是否成功
	 */
	public boolean update(Integer homeworkID, String content) {

		return update(homeworkID, content, null, null, null, null, null, null);
	}

	/**
	 * 更改作业，同时修改附件
	 * 
	 * @param homeworkID 作业ID
	 * @param content 答案内容，null
	 * @param file 文件（以下三个为struts2文件上传必须涉及的三个相关变量）
	 * @param fileName 文件名
	 * @param contentType 文件类型
	 * @param newFileName 新文件名，如果不需要重命名则null
	 * @param description 文件描述，null
	 * @param userID 文件上传者id
	 * @return
	 */
	public boolean update(Integer homeworkID, String content, File file,
			String fileName, String contentType, String newFileName,
			String description, String userID) {

		boolean result = true;
		if (file != null) {
			result = fileService.save(file, fileName, contentType, newFileName,
					description, FileService.TYPE_HOMEWORK_ATTACH_FILE, userID);
		}
		try {
			Homework homework = homeworkDAO.findById(homeworkID);
			if (homework == null) {
				logger.debug("没有找到对应作业");
				return false;
			}
			homework.setContent(content);
			homework.setTime(TimeUtil.getTimeStamp());
			if (file != null) {
				homework.setFileInfo(getFileInfo());
			}
			homeworkDAO.merge(homework);
		} catch (Exception e) {
			// TODO: handle exception
			result = false;
		}
		return result;
	}

	/**
	 * 
	 * @param homework 包含了作业的基本信息，包括用户id和task id 信息
	 * @param file
	 * @param fileName
	 * @param contentType
	 * @param newFileName
	 * @return
	 */
	public boolean save(Homework homework, File file, String fileName,
			String contentType, String newFileName) {

		return save(homework.getUser().getId(), homework.getTask().getId(),
				homework.getContent(), homework.getNote(), file, fileName,
				contentType, newFileName, null);
	}

	/**
	 * 保存作业信息，适合第一次提交作业时调用，只有答案不包含附件
	 * 
	 * @param userID 用户id
	 * @param taskID 布置的作业的id
	 * @param content 答案内容，可为空但木有意义
	 * @param note 备注，可为空
	 * @return 操作结果
	 */
	public boolean save(String userID, Integer taskID, String content,
			String note) {

		return save(userID, taskID, content, note, null, null, null, null, null);
	}

	/**
	 * 保存作业信息，适合第一次提交作业时调用，包含附件
	 * 
	 * @param userID 用户id
	 * @param taskID 布置的作业的id
	 * @param content 答案内容，null
	 * @param note 备注，null
	 * @param file 文件（以下三个为struts2文件上传必须涉及的三个相关变量）
	 * @param fileName 文件名
	 * @param contentType 文件类型
	 * @param newFileName 新文件名 如果不需要重命名则null
	 * @param description 文件描述，null
	 * @return 操作结果
	 */
	public boolean save(String userID, Integer taskID, String content,
			String note, File file, String fileName, String contentType,
			String newFileName, String description) {

		logger.debug("保存作业：user=" + userID + "taskid=" + taskID + "content="
				+ content);
		boolean result = true;
		if (file != null) {
			result = fileService.save(file, fileName, contentType, newFileName,
					description, FileService.TYPE_HOMEWORK_ATTACH_FILE, userID);
		}
		try {
			if (result) {
				fileInfo = fileService.getFileInfo();
				Task task = new Task();
				task.setId(taskID);
				User user = new User();
				user.setId(userID);

				homework = new Homework();
				homework.setTask(task);
				homework.setUser(user);
				homework.setContent(content);
				homework.setNote(note);
				homework.setTime(TimeUtil.getTimeStamp());
				//if (fileInfo != null) {
				homework.setFileInfo(fileInfo);
				//}
				homeworkDAO.attachDirty(homework);
			}
		} catch (Exception e) {
			// TODO: handle exception
			logger.debug("保存作业（提交）失败", e);
			result = false;
		}

		return result;
	}

	/**
	 * 删除作业
	 * 
	 * @param id 作业ID
	 * @return 操作结果
	 */
	public boolean delete(Integer id) {

		logger.debug("删除作业：id=" + id);
		boolean result = true;
		try {
			Homework homework = homeworkDAO.findById(id);
			if (homework == null) {
				logger.debug("没有找到作业信息");
				return false;
			}
			fileInfo = homework.getFileInfo();
			homeworkDAO.delete(homework);
			if (fileInfo == null) {
				logger.debug("没有找到附件");

			} else {
				fileService.delete(fileInfo.getId());
			}
		} catch (Exception e) {
			// TODO: handle exception
			logger.debug("删除作业失败");
			result = false;
		}

		return result;

	}

	public Homework find(Integer id) {

		return homeworkDAO.findById(id);
	}
}
